package com.origin.ebuy.bargd.service.user.impl;

import java.sql.SQLException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.origin.ebuy.bargd.dao.user.IUserDao;
import com.origin.ebuy.bargd.dao.user.impl.UserDaoImpl;
import com.origin.ebuy.bargd.service.user.IUserService;

public class UserServiceImpl implements IUserService {

	IUserDao dao = new UserDaoImpl();
	
	/**
	 * 检测用户名是否可用
	 */
	@Override
	public int queryUserByName(String name) throws SQLException {
		JSONObject jsonObject = dao.queryUserByName(name);
		return jsonObject.isEmpty()?-1:1;
	}

	/**
	 * 用户注册
	 */
	@Override
	public int userRegiste(String name, String passwd) throws SQLException {
		// TODO Auto-generated method stub
		return dao.insertUser(name, passwd);
	}

	/**
	 * 用户登陆
	 */
	@Override
	public JSONObject userLogin(String name, String passwd) throws SQLException {
		JSONObject jsonObject = dao.queryUserByName(name);
		//根据用户名称没有查询到密码
		if(jsonObject.isEmpty()){
			return jsonObject;
		}else{
			//用户名和密码匹配
			if(passwd.equals(jsonObject.getString("passswd"))){
				return jsonObject;
			}
		}
		return new JSONObject();
	}

	@Override
	public JSONArray queryAllUser() throws SQLException {
		return dao.queryAllUser();
	}

	/**
	 * 更新用户的角色
	 */
	@Override
	public int updateUserRole(String id, String rule) throws SQLException {
		String[] cols = {"rule"};
		Object[] params = {rule,id};
		return dao.updateUser("id=?", cols, params);
	}

	/**
	 * 更新用户的状态
	 */
	@Override
	public int updateUserState(String id, String state) throws SQLException {
		String[] cols = {"state"};
		Object[] params = {state,id};
		return dao.updateUser("id=?", cols, params);
	}

}
